import { lazy } from "react"
import { createBrowserRouter, Outlet } from "react-router-dom"
// 导入路由
const Layout = lazy(() => import("@/components/Layout"))
const Alist = lazy(() => import("@/page/Article/Alist"))
const Acreate = lazy(() => import("@/page/Article/Acreate"))
const Blist = lazy(() => import("@/page/Blogs/Blist"))
const Bcreate = lazy(() => import("@/page/Blogs/Bcreate"))
const Plist = lazy(() => import("@/page/Pages/Plist"))
const Pcreate = lazy(() => import("@/page/Pages/Pcreate"))
const PosteList = lazy(() => import("@/page/Poster/PostList"))
const PostCreate = lazy(() => import("@/page/Poster/PostCreate"))
const Ulist = lazy(() => import("@/page/User/Ulist"))
const Ucreate = lazy(() => import("@/page/User/Ucreate"))
// 搭建路由表
export const routeList = [
  {
    path: "/",
    element: <Layout />,
    meta: {
      hidden: true,
    },
    children: [
      // 文章管理
      {
        path: "article",
        element: <Outlet />,
        meta: {
          title: "文章管理",
          authrId: [1, 2],
        },
        children: [
          {
            index: true,
            path: "list",
            element: <Alist />,
            meta: {
              title: "文章列表",
              authrId: [1, 2],
            },
          },
          {
            path: "create",
            element: <Acreate />,
            meta: {
              title: "创建文章",
              authrId: [1, 2],
            },
          },
        ],
      },
      // 博客管理
      {
        path: "blogs",
        element: <Outlet />,
        meta: {
          title: "博客管理",
          authrId: [1, 2],
        },
        children: [
          {
            path: "list",
            element: <Blist />,
            meta: {
              title: "博客列表",
              authrId: [1, 2],
            },
          },
          {
            path: "create",
            element: <Bcreate />,
            meta: {
              title: "创建博客",
              authrId: [1, 2],
            },
          },
        ],
      },
      // 页面管理
      {
        path: "pages",
        element: <Outlet />,
        meta: {
          title: "页面管理",
          authrId: [1, 2],
        },
        children: [
          {
            path: "list",
            element: <Plist />,
            meta: {
              title: "页面列表",
              authrId: [1, 2],
            },
          },
          {
            path: "create",
            element: <Pcreate />,
            meta: {
              title: "创建页面",
              authrId: [1, 2],
            },
          },
        ],
      },
      // 海报管理
      {
        path: "poster",
        element: <Outlet />,
        meta: {
          title: "海报管理",
          authrId: [1, 2],
        },
        children: [
          {
            path: "list",
            element: <PosteList />,
            meta: {
              title: "海报列表",
              authrId: [1, 2],
            },
          },
          {
            path: "create",
            element: <PostCreate />,
            meta: {
              title: "创建海报",
              authrId: [1, 2],
            },
          },
        ],
      },
      // 用户管理
      {
        path: "user",
        element: <Outlet />,
        meta: {
          title: "用户管理",
          authrId: [1],
        },
        children: [
          {
            path: "list",
            element: <Ulist />,
            meta: {
              title: "用户列表",
              authrId: [1],
            },
          },
          {
            path: "create",
            element: <Ucreate />,
            meta: {
              title: "创建用户",
              authrId: [1],
            },
          },
        ],
      },
    ],
  },
]
const router = createBrowserRouter(routeList)
export default router
// 筛选路由的函数
export const filterRouter = (routes: any[], role: number): any[] => {
  return routes
    .map(item => {
      if (!item.meta?.authrId.includes(role)) {
        // 如果权限不足返回空
        return null
      }
      if (item.children) {
        // 如果有子路由
        // 筛选子路由
        const childrenFilter = filterRouter(item.children, role)
        if (childrenFilter.length > 0) {
          // 如果筛选出来的数据长度大于一 返回子路由的信息
          return {
            ...item,
            children: childrenFilter,
          }
        }
        // 小于0则返回空
        return null
      }
      // 返回拥有权限的路由
      return item
      // 过滤所有 null
    })
    .filter(Boolean)
}
// 替换符合Menu菜单的数据
export const MenuRouter = (routes: any[], parentPath: string = ""): any[] => {
  // 筛选出路由原信息中没有hidden的数据
  return (
    routes
      .filter(item => !item.meta?.hidden)
      .map(item => {
        // 拼接完整路径
        const fullPath = `${parentPath}/${item.path}`
        // 返回数据
        return {
          key: fullPath,
          label: item.meta?.title,
          children: item.children
            ? MenuRouter(item.children, item.path)
            : undefined,
        }
      })
      // 过滤所有undefined
      .filter(Boolean)
  )
}
